package com.thoughtworks.android.androsphere.controllers;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.thoughtworks.android.androsphere.model.RegistrationRequest;
import com.thoughtworks.android.androsphere.service.registration.RegistrationService;

@Controller
public class RegistrationController {
    private static final Logger logger = Logger.getLogger(RegistrationController.class);

    private RegistrationService registrationService;

    @Autowired
    public RegistrationController(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }

    @RequestMapping(value = "/register", method = RequestMethod.PUT)
    public void register(@RequestBody RegistrationRequest registrationRequest, HttpServletResponse response) {
        logger.debug("Got request : " + registrationRequest);    

        registrationService.register(registrationRequest);
        response.setStatus(HttpServletResponse.SC_CREATED);
    }

    @RequestMapping(value = "/unregister", method = RequestMethod.DELETE)
    public void unRegister(RegistrationRequest registrationRequest, HttpServletResponse response) {
        registrationService.unRegister(registrationRequest.getDevice());
    }
}
